#include "resourceinput.h"
#include "resourcewindow.h"


namespace gunsmith
{
OIS::InputManager*	CResourceInput::m_pInputManager = NULL;
OIS::DeviceList		CResourceInput::m_xDevices;

CResourceInput::CResourceInput() : CResource(RT_INPUT)
{
	m_pKeyboard	= NULL;
	m_pMouse	= NULL;
	m_pJoyStick	= NULL;
}

CResourceInput::~CResourceInput()
{

}

/*!	\brief		Creates an input resource and retrieves all available devices plugged in.
	\param		pSource	Not used.
	\return		bool
	\remarks	
	\sa			*/
bool CResourceInput::Create(void *pSource)
{
	// get window resource and fetch hWnd
	CResource **ppResources = NULL;
	m_pResMngr->GetResourcesByName("display_window", NULL, &ppResources);
	
	HWND hWnd = ((CResourceWindow*)ppResources[0])->GetInfo()->hWnd;
	delete [] ppResources;	// delete resource array

	// create input manager
	m_pInputManager = InputManager::createInputSystem((size_t)hWnd);
	m_xDevices		= m_pInputManager->listFreeDevices();

	GetInputDevices();

	return (m_pInputManager != NULL); 
}

/*!	\brief		Releases input devices.
	\return		void
	\remarks	
	\sa			*/
void CResourceInput::Destroy()
{
	// destroy created objects
	m_pInputManager->destroyInputObject(m_pKeyboard);
	m_pInputManager->destroyInputObject(m_pMouse);
	m_pInputManager->destroyInputObject(m_pJoyStick);
	InputManager::destroyInputSystem(m_pInputManager);
}

Keyboard* CResourceInput::GetKeyboard() const
{
	return m_pKeyboard;
}

Mouse* CResourceInput::GetMouse() const
{
	return m_pMouse;
}

JoyStick* CResourceInput::GetJoyStick() const
{
	return m_pJoyStick;
}

void CResourceInput::GetInputDevices()
{
	// create input devices and set event callback
	for(DeviceList::iterator itDev = m_xDevices.begin();
		itDev != m_xDevices.end();
		itDev++)
	{
		switch((*itDev).first)
		{
		case OISKeyboard:
			if(!m_pKeyboard)
			{ 
				m_pKeyboard = (Keyboard*)m_pInputManager->createInputObject(OISKeyboard, true);
				m_pKeyboard->setEventCallback(this);
				printf_s("Keyboard found: %s\n", m_pKeyboard->vendor().c_str());
			}
			break;
		case OISMouse:
			if(!m_pMouse)
			{
				m_pMouse = (Mouse*)m_pInputManager->createInputObject(OISMouse, true);
				m_pMouse->setEventCallback(this);
				printf_s("Mouse found: %s\n", m_pMouse->vendor().c_str());
			}
			break;
		case OISJoyStick:
			if(!m_pJoyStick)
			{
				m_pJoyStick = (JoyStick*)m_pInputManager->createInputObject(OISJoyStick, true);
				m_pJoyStick->setEventCallback(this);
				printf_s("JoyStick found: %s\n", m_pJoyStick->vendor().c_str());
			}
			break;
		};
	}
}

bool CResourceInput::keyPressed(const KeyEvent &arg)
{
	printf_s("Keyboard button: %s\n", ((Keyboard*)arg.device)->getAsString(arg.key).c_str() );
	
	if(arg.key == 1)
	{
		CResource **ppResource = NULL;
		m_pResMngr->GetResourcesByName(RID_DISPLAY_WINDOW, NULL, &ppResource);
		*(((CResourceWindow*)ppResource[0])->GetInfo()->pRunning) = false;	// set application running to false

		delete [] ppResource;
	}
	
	return true;
}

bool CResourceInput::keyReleased(const KeyEvent &arg)
{
	return true;
}

bool CResourceInput::mouseMoved(const MouseEvent &arg)
{
	printf_s("Mouse moved: %d, %d\n", arg.state.X.rel, arg.state.Y.rel);
	return true;
}

bool CResourceInput::mousePressed(const MouseEvent &arg, MouseButtonID id)
{
	printf_s("Mouse button: %d\n", id);
	return true;
}

bool CResourceInput::mouseReleased(const MouseEvent &arg, MouseButtonID id)
{
	return true;
}

bool CResourceInput::buttonPressed(const JoyStickEvent &arg, int button)
{
	printf_s("Joystick button: %d\n", button);
	return true;
}

bool CResourceInput::buttonReleased(const JoyStickEvent &arg, int button)
{
	return true;
}

bool CResourceInput::axisMoved(const JoyStickEvent &arg, int axis)
{
	printf_s("Joystick axis(%d): %d\n", axis, arg.state.mAxes[axis].abs);
	return true;
}
};

